<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>公式大全</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 30px;
            background-color: #f0f2f5;
            display: flex;
            justify-content: center;
        }
        .main-container {
            width: 100%;
            max-width: 1400px;
            display: flex;
            gap: 30px;
        }
        #formulaContainer {
            width: 30%;
            height: calc(100vh - 60px);
            display: flex;
            flex-direction: column;
            gap: 15px;
            padding: 15px;
            background-color: #ffffff;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            position: relative;
            overflow-y: auto;
        }
        #content {
            width: 70%;
            height: calc(100vh - 60px);
            margin-top: 0;
            padding: 25px;
            background-color: #ffffff;
            border: 1px solid #e0e3e7;
            border-radius: 8px;
            display: flex;
            flex-direction: column;
            gap: 25px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            overflow-y: auto;
        }
        @media (max-width: 768px) {
            body {
                padding: 15px;
                flex-direction: column;
            }
            .main-container {
                flex-direction: column;
                gap: 15px;
            }
            #formulaContainer, #content {
                width: 100%;
                height: auto;
            }
            #formulaContainer {
                height: 50vh;
            }
            #content {
                height: 50vh;
            }
            .formula-category {
                font-size: 14px;
                padding: 10px;
            }
            .formula-item {
                font-size: 14px;
                padding: 8px;
            }
            #searchBox {
                width: calc(100% - 60px);
                padding: 8px 30px 8px 30px;
                font-size: 13px;
            }
            .clear-button {
                right: 15px;
                top: 30%;
                font-size: 18px;
            }
            .help-button {
                top: 15px;
                right: 15px;
                width: 25px;
                height: 25px;
                font-size: 13px;
            }
            .help-info {
                top: 50px;
                right: 15px;
                width: 180px;
                padding: 10px;
            }
        }
        .formula-category {
            cursor: pointer;
            padding: 12px;
            background-color: #f8f9fa;
            color: #2d3748;
            font-weight: 600;
            border-radius: 6px;
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            list-style-type: none;
        }
        .formula-category:hover {
            background-color: #e9ecef;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
        }
        .formula-list {
            display: none;
            padding: 10px;
            margin: 10px 0;
            list-style-type: none;
            background-color: #ffffff;
            border-radius: 6px;
            border: 1px solid #e0e3e7;
        }
        .formula-item {
            cursor: pointer;
            padding: 10px;
            margin: 5px 0;
            color: #2d3748;
            transition: all 0.3s ease;
            border-radius: 6px;
            background-color: #f8f9fa;
            list-style-type: none;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        }
        .formula-item:hover {
            background-color: #e9ecef;
            transform: translateX(5px);
        }
        .selected {
            color: #007bff;
            font-weight: 600;
            background-color: #e3f2fd;
        }
        #formulaContent {
            background-color: #fcfcfd;
            padding: 25px;
            border-radius: 8px;
            font-size: 20px;
        }
        #formulaRemark {
            background-color: #fcfcfd;
            padding: 25px;
            border-radius: 8px;
        }
        #searchBox {
            width: calc(100% - 80px);
            padding: 10px 40px 10px 40px;
            margin-bottom: 15px;
            border: 2px solid #e0e3e7;
            border-radius: 25px;
            font-size: 14px;
            background-color: #f8f9fa;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='%23999' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='11' cy='11' r='8'%3E%3C/circle%3E%3Cline x1='21' y1='21' x2='16.65' y2='16.65'%3E%3C/line%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: 12px center;
            background-size: 16px;
            transition: all 0.3s ease;
        }

        .clear-button {
            position: absolute;
            right: 20px;
            top: 34%;
            transform: translateY(-50%);
            cursor: pointer;
            color: #999;
            font-size: 20px;
            transition: color 0.3s ease;
            line-height: 1;
        }

        .clear-button:hover {
            color: #666;
        }

        #searchBox:hover {
            border-color: #007bff;
            background-color: #ffffff;
        }

        #searchBox:focus {
            outline: none;
            border-color: #007bff;
            box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.1);
            background-color: #ffffff;
        }

        .help-button {
            position: fixed;
            top: 20px;
            right: 20px;
            width: 30px;
            height: 30px;
            background-color: #999;
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            font-size: 15px;
            transition: background-color 0.3s ease;
        }

        .help-button:hover {
            background-color: #666;
        }

        .help-info {
            position: fixed;
            top: 60px;
            right: 20px;
            width: 200px;
            padding: 12px;
            background-color: white;
            border: 1px solid #e0e3e7;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            display: none;
        }

        .help-info a {
            color: #007bff;
            text-decoration: none;
        }

        .help-info a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="main-container">
        <div class="help-button" onclick="toggleHelpInfo()">?</div>
        <div class="help-info" id="helpInfo">
            <p>温馨提示：</p>
            <p>补充内容可以使用 <a href="https://www.bejson.com/md/">在线markdown</a> 网站来编辑后复制进来。</p>
        </div>
        <div id="formulaContainer">
            <div style="position: relative;">
                <input type="text" id="searchBox" placeholder="输入公式名称回车搜索...">
                <span class="clear-button" onclick="document.getElementById('searchBox').value = ''; document.getElementById('searchBox').dispatchEvent(new Event('input'));">×</span>
            </div>
            <div id="formulaListContainer"></div>
        </div>
        <div id="content">
            <div id="formulaContent"></div>
            <div id="formulaRemark"></div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    <script>
        const jsonData = `[
            {
                "formulaClassName": "小学公式",
                "formulaList": [
                    {
                        "formulaName": "基础加减法",
                        "formulaContent": "$\\\\displaylines{加法公式：\\\\qquad 加数+加数=和 \\\\\\\\ \\\\\\\\ 减法公式：\\\\qquad 被减数-减数=差}$",
                        "formulaRemark": "### 加法公式\\n\\n##### 示例\\n\\n示例1： 1+2=3\\n\\n解释：1和2都是加数，3是和\\n\\n\\n示例2： 2+1=3\\n\\n解释：1和2都是加数，3是和\\n\\n### 减法公式\\n\\n##### 示例\\n\\n示例1： 3-1=2\\n\\n解释：3是被减数，1是减数，2是差\\n\\n示例2： 3-2=1\\n\\n解释：3是被减数，2是减数，1是差\\n\\n&zwnj;**关于理解问题可随机生成计算题多让孩子做做题**&zwnj;"
                    },
                    {
                        "formulaName": "三角形面积公式",
                        "formulaContent": "$三角形面积公式： 长*宽=面积 三角形面积公式2： 长*宽=面积$",
                        "formulaRemark": "其他的补充内容"
                    }
                ]
            },
            {
                "formulaClassName": "初中公式",
                "formulaList": [
                    {
                        "formulaName": "完全平方公式",
                        "formulaContent": "公式内容",
                        "formulaRemark": "其他的补充内容"
                    }
                ]
            },
            {
                "formulaClassName": "大学公式",
                "formulaList": [
                    {
                        "formulaName": "微积分方程式",
                        "formulaContent": "公式内容2",
                        "formulaRemark": "其他的补充内容222"
                    }
                ]
            }
        ]`;

        const data = JSON.parse(jsonData); // 确保 jsonData 被正确解析为 JSON 对象
        const formulaContainer = document.getElementById('formulaContainer');
        const formulaContent = document.getElementById('formulaContent');
        const formulaRemark = document.getElementById('formulaRemark');
        const searchBox = document.getElementById('searchBox');
        const formulaListContainer = document.getElementById('formulaListContainer');

        searchBox.addEventListener('input', (e) => {
            const searchText = e.target.value;
            const filteredCategories = data.map(category => {
                return {
                    ...category,
                    formulaList: category.formulaList.filter(formula => 
                        formula.formulaName.toLowerCase().includes(searchText.toLowerCase())
                    )
                };
            }).filter(category => category.formulaList.length > 0);
            renderFilteredCategories(filteredCategories);
        });

        searchBox.addEventListener('keydown', (e) => {
            if (e.key === 'Enter') {
                const searchText = e.target.value;
                const filteredCategories = data.map(category => {
                    return {
                        ...category,
                        formulaList: category.formulaList.filter(formula => 
                            formula.formulaName.toLowerCase().includes(searchText.toLowerCase())
                        )
                    };
                }).filter(category => category.formulaList.length > 0);
                renderFilteredCategories(filteredCategories);
            }
        });

        function renderFilteredCategories(filteredCategories) {
            formulaListContainer.innerHTML = ''; // 清空公式列表容器

            filteredCategories.forEach((category, categoryIndex) => {
                const categoryElement = document.createElement('div');
                categoryElement.className = 'formula-category';
                categoryElement.textContent = category.formulaClassName;

                const formulaListElement = document.createElement('div');
                formulaListElement.className = 'formula-list';
                formulaListElement.style.display = searchBox.value.trim() === '' ? 'none' : 'block'; // 当输入框内容为空时，默认折叠

                category.formulaList.forEach((formula, formulaIndex) => {
                    const formulaItem = document.createElement('div');
                    formulaItem.className = 'formula-item';
                    formulaItem.textContent = formula.formulaName;
                    formulaItem.addEventListener('click', () => {
                        formulaContent.innerHTML = `
                            <h2>${formula.formulaName}</h2>
                            <p>${formula.formulaContent}</p>
                        `;
                        MathJax.typesetPromise([formulaContent]).then(() => {
                            console.log('MathJax rendering complete');
                        }).catch((err) => console.log('MathJax rendering error', err));
                        formulaRemark.innerHTML = marked.parse(formula.formulaRemark); // 使用 marked.js 解析 Markdown
                        document.querySelectorAll('.formula-item').forEach(item => item.classList.remove('selected'));
                        formulaItem.classList.add('selected');
                    });

                    formulaListElement.appendChild(formulaItem);
                });

                categoryElement.addEventListener('click', () => {
                    formulaListElement.style.display = formulaListElement.style.display === 'block' ? 'none' : 'block';
                });

                formulaListContainer.appendChild(categoryElement);
                formulaListContainer.appendChild(formulaListElement);
            });
        }

        function renderFormulas(searchText = '') {
            formulaListContainer.innerHTML = ''; // 清空公式列表容器

            data.forEach((category, categoryIndex) => {
                const categoryElement = document.createElement('div');
                categoryElement.className = 'formula-category';
                categoryElement.textContent = category.formulaClassName;

                const formulaListElement = document.createElement('div');
                formulaListElement.className = 'formula-list';

                category.formulaList.forEach((formula, formulaIndex) => {
                    if (searchText && !formula.formulaName.toLowerCase().includes(searchText.toLowerCase())) {
                        return;
                    }

                    const formulaItem = document.createElement('div');
                    formulaItem.className = 'formula-item';
                    formulaItem.textContent = formula.formulaName;
                    formulaItem.addEventListener('click', () => {
                        formulaContent.innerHTML = `
                            <h2>${formula.formulaName}</h2>
                            <p>$${formula.formulaContent}$</p>
                        `;
                        MathJax.typesetPromise([formulaContent]).then(() => {
                            console.log('MathJax rendering complete');
                        }).catch((err) => console.log('MathJax rendering error', err));
                        formulaRemark.innerHTML = marked.parse(formula.formulaRemark); // 使用 marked.js 解析 Markdown
                        document.querySelectorAll('.formula-item').forEach(item => item.classList.remove('selected'));
                        formulaItem.classList.add('selected');
                    });

                    // 默认渲染第一个分类的第一个公式
                    if (categoryIndex === 0 && formulaIndex === 0 && !searchText) {
                        formulaContent.innerHTML = `
                            <h2>${formula.formulaName}</h2>
                            <p>$${formula.formulaContent}$</p>
                        `;
                        MathJax.typesetPromise([formulaContent]).then(() => {
                            console.log('MathJax rendering complete');
                        }).catch((err) => console.log('MathJax rendering error', err));
                        formulaRemark.innerHTML = marked.parse(formula.formulaRemark);
                        formulaItem.classList.add('selected');
                    }

                    formulaListElement.appendChild(formulaItem);
                });

                categoryElement.addEventListener('click', () => {
                    formulaListElement.style.display = formulaListElement.style.display === 'block' ? 'none' : 'block';
                });

                formulaListContainer.appendChild(categoryElement);
                formulaListContainer.appendChild(formulaListElement);
            });
        }

        function toggleHelpInfo() {
            const helpInfo = document.getElementById('helpInfo');
            helpInfo.style.display = helpInfo.style.display === 'block' ? 'none' : 'block';
        }

        renderFormulas();
    </script>

</body>
</html>